/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.dp.view;

import com.dp.dto.BookListDTO;
import com.dp.controller.BookListController;
import com.dp.model.Books;
import com.dp.utils.BaseException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Onur
 */
public class BookList extends AbstractPage<BookListDTO> {

    Vector<Object> columnNames = new Vector<Object>();
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    
    /**
     * Creates new form BookList
     */
    public BookList() {
        initComponents();
        
        columnNames.add("objid");
        columnNames.add("Type");
        columnNames.add("Year");
        columnNames.add("Book Name");
        columnNames.add("Publisher");
        columnNames.add("ISBN");
        
        loadAllBookList();
    }

    public void loadAllBookList() {
        
        try {
            List<Books> books = BookListController.loadBookList(dto);
            
            loadTableData(books);
            
        } catch (BaseException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Hata", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void loadTableData(List<Books> books) {
        data.clear();
        
        for(Books book : books) {
            Vector<Object> vector = new Vector<Object>();

            vector.add(book.getObjid());
            vector.add(" " + book.getIssueArea());

            if(book.getIssueDate() != null ) {
                Calendar c = new GregorianCalendar();
                c.setTime(book.getIssueDate());
                vector.add(" " + c.get(Calendar.YEAR));
            }
            
            vector.add(" " + book.getBookName());
            vector.add(" " + book.getPublisherName());
            vector.add(" " + book.getIsbn());

            data.add(vector);
        }

        jTable2.setModel(new DefaultTableModel(data, columnNames));
        jTable2.removeColumn(jTable2.getColumnModel().getColumn(0));
        
        jTable2.getColumnModel().getColumn(0).setMinWidth(80);
        jTable2.getColumnModel().getColumn(1).setMaxWidth(45);
        jTable2.getColumnModel().getColumn(2).setMinWidth(270);
        jTable2.getColumnModel().getColumn(4).setMinWidth(105);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnDeleteBook = new javax.swing.JButton();
        btnUpdateBook = new javax.swing.JButton();
        btnAddBook = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtfBookName = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        btnSearch = new javax.swing.JButton();

        setBackground(new java.awt.Color(235, 244, 249));
        setPreferredSize(new java.awt.Dimension(472, 550));

        btnDeleteBook.setText("Delete");
        btnDeleteBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteBookActionPerformed(evt);
            }
        });

        btnUpdateBook.setText("Modify");
        btnUpdateBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateBookActionPerformed(evt);
            }
        });

        btnAddBook.setText("Add");
        btnAddBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddBookActionPerformed(evt);
            }
        });

        jLabel1.setForeground(new java.awt.Color(25, 84, 107));
        jLabel1.setText("Book Records");

        jLabel3.setText("Book name:");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable2.setRowHeight(18);
        jTable2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable2.setShowVerticalLines(false);
        jScrollPane2.setViewportView(jTable2);

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfBookName, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSearch))
                    .addComponent(jScrollPane2)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnAddBook, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(btnUpdateBook, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(btnDeleteBook)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfBookName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDeleteBook)
                    .addComponent(btnUpdateBook)
                    .addComponent(btnAddBook))
                .addGap(7, 7, 7))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddBookActionPerformed
        // TODO add your handling code here:
        MainApplication.getInstance().navigate(AbstractPage.getInstance(BookAdd.class));
    }//GEN-LAST:event_btnAddBookActionPerformed

    private void btnUpdateBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateBookActionPerformed
        // TODO add your handling code here:
        
        int row = jTable2.getSelectedRow();
        if(row == -1) {
            JOptionPane.showMessageDialog(this, "Please select an item.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        Long objid = (Long)jTable2.getModel().getValueAt(row, 0);
        
        BookAdd bookAdd = (BookAdd) AbstractPage.getInstance(BookAdd.class);
        bookAdd.getDto().setObjid(objid);
        bookAdd.loadBookInfo();
        
        MainApplication.getInstance().navigate(bookAdd);
    }//GEN-LAST:event_btnUpdateBookActionPerformed

    private void btnDeleteBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteBookActionPerformed
        // TODO add your handling code here:
        
        int row = jTable2.getSelectedRow();
        if(row == -1) {
            JOptionPane.showMessageDialog(this, "Please select an item.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
    
        Long objid = (Long)jTable2.getModel().getValueAt(row, 0);
                
        if(JOptionPane.OK_OPTION == 
                JOptionPane.showConfirmDialog(
                        this, "Operation will be processed. Do you confirm?", "Confirmation", JOptionPane.OK_CANCEL_OPTION)) {
            try {
                dto.setObjid(objid);
                BookListController.deleteBookByObjid(dto);
                ((DefaultTableModel)jTable2.getModel()).removeRow(row);
            } catch (BaseException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnDeleteBookActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        
        try {
            List<Books> books = null;
            
            String bookName = jtfBookName.getText();
            if(bookName.isEmpty()) {
                books = BookListController.loadBookList(dto);
            }
            else {
                dto.setBookName(bookName);
                books = BookListController.getBooksByName(dto);
            }
            
            loadTableData(books);
            
        } catch (BaseException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnSearchActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddBook;
    private javax.swing.JButton btnDeleteBook;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnUpdateBook;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jtfBookName;
    // End of variables declaration//GEN-END:variables
}
